package wxlogin

import (
	global2 "counter-help/admin/server/global"
	"counter-help/admin/server/plugin/wxlogin/global"
	"counter-help/admin/server/plugin/wxlogin/model"
	"counter-help/admin/server/plugin/wxlogin/passport"
	"counter-help/admin/server/plugin/wxlogin/router"
	"fmt"
	"github.com/gin-gonic/gin"
	"time"
)

var wxLoginPassport = new(passport.WxLoginPassport)

type WxLoginPlugin struct {
}

func CreateWxLoginPlug(AppID, Appsecret, Token string, AuthorityID uint) *WxLoginPlugin {
	global.WxGlobal.AppID = AppID
	global.WxGlobal.Appsecret = Appsecret
	global.WxGlobal.Token = Token
	global.WxGlobal.AuthorityID = AuthorityID

	global2.GVA_DB.AutoMigrate(model.WXUserInfo{})
	if global.WxGlobal.AccessToken == "" {
		go func() {
			err := wxLoginPassport.ReflashAccessToken()
			if err != nil {
				fmt.Println(err)
			}
		}()
	}

	go func() {
		for {
			time.Sleep(300 * time.Second)
			global.LoginCodeMap.Range(func(key, value interface{}) bool {
				if time.Now().Unix()-value.(global.CodeInfo).CreateTime > 300 {
					global.LoginCodeMap.Delete(key)
				}
				return false
			})
		}
	}()
	return &WxLoginPlugin{}
}

func (*WxLoginPlugin) Register(group *gin.RouterGroup) {
	router.RouterGroupApp.InitWxLoginRouter(group)
}

func (*WxLoginPlugin) RouterPath() string {
	return "wxLogin"
}
